# Sustainable Office Designer
# by Fabian Ritter <Fabian.Ritter@live.de>, 2012

require("SOD/SODxml/SODxml.dll")
require("SOD/tools/OpenStudio/Collection")

module SOD
  
  class FileManager
    
    attr_reader :read_values
    
    def initialize
      @bars = Collection.new
      @file = SODxml::File.new
    end
    
    def get_su_point_from_xmlpoint( p )
      point = Geom::Point3d.new( p.x, p.y, p.z )
      return point
    end
    
    def get_su_vec_from_xmlpoint( p )
      point = Geom::Vector3d.new( p.x, p.y, p.z )
      return point
    end
    
    def get_xml_point_from_su( p )
      point = SODxml::Point.new( p.x, p.y, p.z )
      return point
    end
    
    def update(bar)
      
      bar.projectInfo.update_values #to be shure all values are up to date
      
      sodBar = SODxml::Bar.new
      
      sodBar.set_NOS( bar.projectInfo.numberOfStoreys )
      xml_origin = get_xml_point_from_su( bar.sketchInfoContainer.pointOfOrigin )
      xml_direction = get_xml_point_from_su( bar.sketchInfoContainer.direction )
      sodBar.set_origin( xml_origin )
      sodBar.set_direction( xml_direction )
      sodBar.set_length( bar.sketchInfoContainer.footprintLength.to_mm )
      sodBar.set_width( bar.sketchInfoContainer.footprintWidth.to_mm )

      #sg_values = []
      #puts sg_values << bar.sgHash.bar_hash[:g_r_vc] # volume concrete
      #puts sg_values << bar.sgHash.bar_hash[:g_r_mr] # mass reinforcement steel
      #puts sg_values << bar.sgHash.bar_hash[:g_r_mp] # mass profiled sheeting
      #puts sg_values << bar.sgHash.bar_hash[:g_r_mq] # mass structure steel
      #puts sg_values << bar.sgHash.bar_hash[:g_r_aa] # area steel coating
      
      @bars.add sodBar
    end
    
    def writeDataToFile(project)
      
      @bars.clear
      sodBuilding = SODxml::Building.new();
      
      #--- set SODxml BAR Values ---------------------------
      Plugin.model.bars.each do |bar|
        update(bar)
      end
      
      barsVec = SODxml::BarVector.new( @bars.length )
      0.upto(@bars.length-1) do |i|
        barsVec[i] = @bars[i]
      end
      
      sodBuilding.Bars(barsVec)
      
      #--- set Project Values for SODxml ---------------------------
      Plugin.model.update_projectValues
      
      vc = Plugin.model.projectValues[:tot_vc] # volume concrete
      mr = Plugin.model.projectValues[:tot_mr] # mass reinforcement steel
      mp = Plugin.model.projectValues[:tot_mp] # mass profiled sheeting
      mq = Plugin.model.projectValues[:tot_mq] # mass structure steel
      aa = Plugin.model.projectValues[:tot_aa] # area steel coating
      
      # Li
      sg_values_double_vec = SODxml::DoubleVector.new( 5 )
      sg_values_double_vec[0] = vc;
      sg_values_double_vec[1] = mr;
      sg_values_double_vec[2] = mp;
      sg_values_double_vec[3] = mq;
      sg_values_double_vec[4] = aa;
      # sodBuilding.SgValues( vc, mr, mp, mq, aa )
      sodBuilding.SgValues(sg_values_double_vec)
      # 
      
      if $debug
        print "\nbuilding.number_of_storeys: #{sodBuilding.Bars()[0].get_NOS()} \n"
        print "building.bars[0].length: #{sodBuilding.Bars()[0].get_length()} \n"
        print "building.bars[0].width: #{sodBuilding.Bars()[0].get_width()} \n"
      end
      
      #path = "C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/SOD/data/1/Variant.xml"
      path = project.activeFilePath + "/" + project.activeFileName + ".xml"
      
      UI.messagebox path.to_s if $debug
      
      puts "error on file.write: "
      puts @file.write(path, sodBuilding)
      
    end
    
    def readFile( project )
      
      @bars.clear
      path = project.activeFilePath + "/" + project.activeFileName + ".xml"
      
      #--- get the data ----------
      building, values = read_values( path )
      
      #--- wirte them to 
      Plugin.model.set_projectValues( values )
      updateRuby( building.Bars().size, building )
      
    end
    
    def read_values( path )
      
      sodBuilding = SODxml::Building.new();
      
      @file.read( path , sodBuilding )
      
      if $debug
        print "\nbuilding.number_of_storeys: #{sodBuilding.Bars()[0].get_NOS()} \n"
        print "building.bars[0].length: #{sodBuilding.Bars()[0].get_length()} \n"
        print "building.bars[0].width: #{sodBuilding.Bars()[0].get_width()} \n"
      end
      
      sg_values_double_vec = sodBuilding.SgValues();
      
      sg_values = []

      sg_values_count = sg_values_double_vec.size()
      puts "sg_values_count: " + sg_values_count.to_s
      0.upto(sg_values_count-1) do |i|
        sg_values << sg_values_double_vec[i]
      end


      sg_values_count.upto(5-1) do |i|
        # sg_values[i] = sg_values_double_vec[i]
        sg_values << 0
      end
      # sg_values << sodBuilding.TOT_vc();
      # sg_values << sodBuilding.TOT_mr();
      # sg_values << sodBuilding.TOT_mp();
      # sg_values << sodBuilding.TOT_mq();
      # sg_values << sodBuilding.TOT_aa();

      return sodBuilding, sg_values
    
    end
    
    def updateRuby( numOfBars, building )
      
      0.upto( numOfBars-1 ) do |i|
        
        barname = "Bar" + (i + 1).to_s
        bar = ModelInterface.new( barname )
        Plugin.model.bars.add bar
        
        su_origin = get_su_point_from_xmlpoint( building.Bars()[i].get_origin() )
        su_direction = get_su_vec_from_xmlpoint( building.Bars()[i].get_direction() )
        bar.sketchInfoContainer.pointOfOrigin = su_origin
        bar.sketchInfoContainer.direction = su_direction
        bar.sketchInfoContainer.footprintLength = building.Bars()[i].get_length()
        bar.sketchInfoContainer.footprintWidth = building.Bars()[i].get_width()
        bar.sketchInfoContainer.footprintLength = bar.sketchInfoContainer.footprintLength.round.mm
        bar.sketchInfoContainer.footprintWidth = bar.sketchInfoContainer.footprintWidth.round.mm
        puts "bar"
        puts bar.sketchInfoContainer.footprintLength.to_s
        puts bar.sketchInfoContainer.footprintWidth.to_s
        bar.projectInfo.numberOfStoreys = building.Bars()[i].get_NOS()
        
        Plugin.model.bars.each do |bar|
          bar.drawFootprint
          #bar.drawVolumes
        end
      
      end
      
    end
    
    def set_default_file(file)
      default_text = "<?xml version=\"1.0\"?>\n" +
        "<!DOCTYPE SOD SYSTEM \"SOD.dtd\">\n" +
	"<BARS>\n" +
	"</BARS>"
      file.write( default_text )
    end
    
  end
  
end
